package com.ruoyi.project.ledger.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.service.PermissionService;
import com.ruoyi.project.ledger.domain.LedgerSafetyManagementInspectionDaily;
import com.ruoyi.project.ledger.domain.LedgerSafetyManagementInspectionDailyContent;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.ledger.domain.LedgerSafetyManagementInspection;
import com.ruoyi.project.ledger.service.ILedgerSafetyManagementInspectionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 安全管理监督检查台账Controller
 *
 * @author gwc
 * @date 2020-04-14
 */
@RestController
@RequestMapping("/ledger/inspection")
public class LedgerSafetyManagementInspectionController extends BaseController
{
    @Autowired
    private ILedgerSafetyManagementInspectionService ledgerSafetyManagementInspectionService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询安全管理监督检查台账列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:inspection:list')")
    @GetMapping("/list")
    public TableDataInfo list(LedgerSafetyManagementInspection ledgerSafetyManagementInspection)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSafetyManagementInspection.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSafetyManagementInspection.setAreas(areas);
        ledgerSafetyManagementInspection.setManagementCharacters(managementCharacters);
        startPage();
        List<LedgerSafetyManagementInspection> list = ledgerSafetyManagementInspectionService.selectLedgerSafetyManagementInspectionList(ledgerSafetyManagementInspection);
        return getDataTable(list);
    }

    /**
     * 导出安全管理监督检查台账列表
     */
//    @PreAuthorize("@ss.hasPermi('ledger:inspection:export')")
    @Log(title = "安全管理监督检查台账", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(LedgerSafetyManagementInspection ledgerSafetyManagementInspection)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 场站id
        Long stationId = user.getStationId();
        if(stationId != null){
            ledgerSafetyManagementInspection.setStationId(stationId);
        }
        // 管辖区域
        List<String> areaList = user.getAreaList();
        String areas = String.join(",", areaList);
        String managementCharacters = user.getManagementCharacters();
        ledgerSafetyManagementInspection.setAreas(areas);
        ledgerSafetyManagementInspection.setManagementCharacters(managementCharacters);
        List<LedgerSafetyManagementInspection> list = ledgerSafetyManagementInspectionService.selectLedgerSafetyManagementInspectionList(ledgerSafetyManagementInspection);
        ExcelUtil<LedgerSafetyManagementInspection> util = new ExcelUtil<LedgerSafetyManagementInspection>(LedgerSafetyManagementInspection.class);
        return util.exportExcel(list, "安全管理监督检查台账");
    }

    /**
     * 获取安全管理监督检查台账详细信息
     */
    /*@PreAuthorize("@ss.hasPermi('ledger:inspection:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ledgerSafetyManagementInspectionService.selectLedgerSafetyManagementInspectionById(id));
    }

    /**
     * 新增安全管理监督检查台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:inspection:add')")
    @Log(title = "安全管理监督检查台账", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LedgerSafetyManagementInspection ledgerSafetyManagementInspection)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            Long stationId = user.getStationId();
            ledgerSafetyManagementInspection.setStationId(stationId);
        }else{
            return AjaxResult.error("当前角色无新增权限");
        }
        ledgerSafetyManagementInspection.setCreateId(user.getUserId().toString());
        return toAjax(ledgerSafetyManagementInspectionService.insertLedgerSafetyManagementInspection(ledgerSafetyManagementInspection));
    }

    /**
     * 修改安全管理监督检查台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:inspection:edit')")
    @Log(title = "安全管理监督检查台账", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LedgerSafetyManagementInspection ledgerSafetyManagementInspection)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){
            Long stationId = user.getStationId();
            ledgerSafetyManagementInspection.setStationId(stationId);
        }else{
            return AjaxResult.error("当前角色无修改权限");
        }
        ledgerSafetyManagementInspection.setUpdateId(user.getUserId().toString());
        return toAjax(ledgerSafetyManagementInspectionService.updateLedgerSafetyManagementInspection(ledgerSafetyManagementInspection));
    }

    /**
     * 删除安全管理监督检查台账
     */
    @PreAuthorize("@ss.hasPermi('ledger:inspection:remove')")
    @Log(title = "安全管理监督检查台账", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ledgerSafetyManagementInspectionService.deleteLedgerSafetyManagementInspectionByIds(ids));
    }

    /**
     * 导出安全管理监督检查台账详细信息
     */
    @Log(title = "安全管理监督检查台账台账", businessType = BusinessType.EXPORT)
    @GetMapping(value = "exportDetail/{id}")
    public AjaxResult exportDetail(@PathVariable("id") Long id)
    {
        return ledgerSafetyManagementInspectionService.exportDetail(id);
    }


    @PreAuthorize("@ss.hasPermi('ledger:inspection:edit')")
    @Log(title = "日常检查整改", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/updateDaily")
    public AjaxResult edit(@RequestBody LedgerSafetyManagementInspectionDaily daily)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 判断是否是场站角色
        if(!permissionService.hasRole("admin") && (permissionService.hasRole("station") || permissionService.hasRole("hs_station"))){

        }else{
            return AjaxResult.error("当前角色无整改权限");
        }
        daily.setUpdateId(user.getUserId().toString());
        return toAjax(ledgerSafetyManagementInspectionService.updateDaily(daily));
    }

    @GetMapping("/list2")
    public TableDataInfo list2()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色判断
        Long stationId = null;
        if(permissionService.hasRole("admin")){    //管理员

        }else if(permissionService.hasRole("station") || permissionService.hasRole("hs_station")){     //场站
            stationId = user.getStationId();
        }
        List<LedgerSafetyManagementInspectionDaily> list = ledgerSafetyManagementInspectionService.selectContentList(stationId);
        if (list != null && list.size() >0){
            for (LedgerSafetyManagementInspectionDaily daily : list){
                daily.setIsProblems("0");
                daily.setRectificationType("0");
                daily.setTermDate(DateUtils.getNowDate());
            }
        }
        return getDataTable(list);
    }
}
